﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Management;
using System.Configuration;

namespace BaseData.Common
{
    /// <summary>
    /// 获取计算机信息
    /// </summary>
    public class ComputerInfo
    {
        /// <summary>
        /// 获取CPU编号
        /// </summary>
        public static string GetCpu()
        {
            string strCpu = string.Empty;
            try
            {
                ManagementClass myCpu = new ManagementClass("win32_Processor");
                ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
                foreach (ManagementObject myObject in myCpuConnection)
                {
                    if (myObject.Properties["Processorid"] != null)
                    {
                        strCpu = myObject.Properties["Processorid"].Value.ToString();
                        break;
                    }
                }
            }
            catch { }
            return strCpu;
        }

        /// <summary>
        /// 获取Mac地址
        /// </summary>
        public static string GetMac()
        {
            string mac = string.Empty;
            try
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                    mo.Dispose();
                }
            }
            catch { }
            return mac;
        }

        /// <summary>
        /// 获取IP地址
        /// </summary>
        public static string GetIP()
        {
            string ipAddress = string.Empty;
            try
            {
                System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                if (ipEntry != null && ipEntry.AddressList != null)
                {
                    for (int i = 0; i < ipEntry.AddressList.Length; i++)
                    {
                        ipAddress = ipEntry.AddressList[i].ToString();
                        if (!ipAddress.Contains("192"))
                        {
                            break;
                        }
                    }
                }
            }
            catch
            {
            }
            return ipAddress;
        }

        /// <summary>
        /// 获取计算机名称
        /// </summary>
        public static string GetName()
        {
            try
            {
                return System.Environment.MachineName;
            }
            catch
            {
                return "unknow";
            }
        } 


        /// <summary>
        /// 判断是否经过授权
        /// </summary>
        public static bool IsAuthorization(bool isSendEmail)
        {
            bool isAuth = false;
            string cpu = GetCpu();
            string mac = GetMac();
            string encyKey = cpu + "shizi" + mac + "sunllly@139.com";
            string encytips = ConfigurationManager.AppSettings["EncyTips"];
            if (!string.IsNullOrEmpty(encytips))
            {
                int sIndex = encytips.IndexOf("HUIHUIGFDF");
                if (sIndex > -1)
                {
                    encytips = encytips.Substring(sIndex+10);
                }
                int eIndex = encytips.IndexOf("0E0BYANYAN");
                if (eIndex > -1)
                {
                    encytips = encytips.Substring(0, eIndex);
                }
            }

            HashEncrypt ecy = new HashEncrypt(HashEncrypt.ServiceProviderEnum.MD5);
            if (ecy.HashString(encyKey) == encytips)
            {
                isAuth = true;
            }
            else if (isSendEmail)
            {
                string message = "出租屋地税管理软件未经授权在使用，使用信息<br/><br/>";
                message += "MAC:" + mac + "<br/>";
                message += "CPU:" + cpu + "<br/>";
                message += "IP:" + GetIP() + "<br/>";
                message += "Name:" + GetName() + "<br/>";
                BaseData.Common.Email.Send("出租屋地税管理软件", message, "sunllly@139.com");
            }
            return isAuth;
        }
    }
}
